import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import '../../../common/index.dart';

/// 植物卡片组件
class PlantCard extends StatelessWidget {
  final Plant? plant;
  final Color card_color;
  final VoidCallback? onTap;

  const PlantCard({
    super.key,
    required this.plant,
    required this.card_color,
    this.onTap,
  });

  /// 计算植物陪伴天数
  int _calculate_days() {
    if (plant?.create_time == null) return 0;

    final createDate = DateTime.parse(plant!.create_time!);
    final today = DateTime.now();
    return today.difference(createDate).inDays;
  }

  @override
  Widget build(BuildContext context) {
    // 如果plant为空，显示占位UI
    if (plant == null) {
      return Container(
        margin: EdgeInsets.only(bottom: 16.h),
        height: 100.h,
        decoration: BoxDecoration(
          color: card_color,
          borderRadius: BorderRadius.circular(20.r),
        ),
        child: Center(
          child: Text(
            "Plant data not available",
            style: TextStyle(color: Colors.black54),
          ),
        ),
      );
    }

    final days = _calculate_days();

    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: EdgeInsets.only(bottom: 16.h),
        height: 100.h,
        decoration: BoxDecoration(
          color: card_color,
          borderRadius: BorderRadius.circular(20.r),
        ),
        child: Row(
          children: [
            SizedBox(width: 25.w),
            Image(
              image: ImagePath().get_post_image(plant!.image),
              width: 73.w,
              height: 83.h,
              fit: BoxFit.fill,
            ),
            SizedBox(width: 25.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    plant!.name,
                    style: TextStyle(
                      fontSize: 24.sp,
                      fontWeight: FontWeight.w900,
                      color: Colors.black,
                    ),
                  ),
                  SizedBox(height: 4.h),
                  RichText(
                    text: TextSpan(
                      style: TextStyle(color: Colors.black),
                      children: [
                        TextSpan(
                          text: "Accompanied for ",
                          style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        TextSpan(
                          text: "$days",
                          style: TextStyle(
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        TextSpan(
                          text: " days",
                          style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
